package com.skl.resp;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class R<T> {
    private Integer code;
    private String msg;
    private T data;
    private Boolean success;

    /**
     * 成功
     * @param data
     * @param <T>
     * @return
     */
    public static <T> R<T> ok(T data){
        return new R<>(C.SUCCESS.getCode(),C.SUCCESS.getMsg(),data,true);
    }

    /**
     * 失败，推荐
     * @param c
     * @param <T>
     * @return
     */
    public static <T> R<T> failed(C c){
        return new R<>(c.getCode(),c.getMsg(),null,false);
    }

    /**
     * 失败，不推荐
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> R<T> failed(int code,String msg){
        return new R<>(code,msg,null,false);
    }

    /**
     * 失败
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> R<T> failed(String msg){
        return new R<>(C.FAILED.getCode(),msg,null,false);
    }

    /**
     * 请求有误，参数或请求方式
     * @param <T>
     * @return
     */
    public static <T> R<T> invalidReq(){
        return failed(C.INVALID_REQ);
    }

    /**
     * 未登录
     * @param <T>
     * @return
     */
    public static <T> R<T> unauthorized(){
        return failed(C.UNAUTHORIZED);
    }

    /**
     * 没有权限
     * @param <T>
     * @return
     */
    public static <T> R<T> forbidden(){
        return failed(C.FORBIDDEN);
    }
}
